<%
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", -1); //prevents caching at the proxy server
%>
<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.text.*"%>
<%@ include file="Connections/conDB.jsp" %>
<% int validaSession = 2; %>
<%@ include file="Connections/session.jsp" %>

<%
String parId_movim = request.getParameter("id");

Driver DriverRs = (Driver)Class.forName(MM_conDB_DRIVER).newInstance();
Connection ConnRs = DriverManager.getConnection(MM_conDB_STRING,MM_conDB_USERNAME,MM_conDB_PASSWORD);
String query ="";
PreparedStatement StatementRs =null;
ResultSet Rsquery = null;
boolean Rsquery_hasData = false;

ResultSet Rsquery2 = null;
String bandera = "0";
String rsColumna = "";

String mensaje = "El Motivo no puede ser eliminado ya que ";
		mensaje += "existen registros en Movimientos (Bitacoras)";

if(parId_movim!=null && !parId_movim.equals("")){
/*	query = "SELECT A.name AS columna, UPPER(A.name) AS nombre, "+
			"PD.descripcion "+
			"FROM syscolumns AS A LEFT OUTER JOIN "+
			"parametros_desc AS PD ON A.name = PD.parametro "+
			"WHERE A.id IN "+
			"(SELECT B.id "+
			"FROM sysobjects AS B "+
			"WHERE B.name = 'parametro') AND "+
			"A.name NOT IN ('id_param') "+
			"ORDER BY PD.descripcion";
	//out.print(query+"<br>");
	StatementRs = ConnRs.prepareStatement(query);
	Rsquery2 = StatementRs.executeQuery();
	while (Rsquery2.next()) {
		rsColumna = Rsquery2.getString("columna");
		
		query = "SELECT "+rsColumna+" FROM parametro";
		//out.print(query+"<br>");
		StatementRs = ConnRs.prepareStatement(query);
		Rsquery = StatementRs.executeQuery();
		if (Rsquery.next()) {
			if (Rsquery.getString(rsColumna).equals(parId_movim)) {
				bandera = "1";
				break;
			} else {
				bandera = "0";
			}
		}
		Rsquery.close();
		//out.print("bandera -> "+bandera+"<br>");
	}
	//out.print("bandera -> "+bandera+"<br>");
	Rsquery2.close();
*/
	if (bandera.equals("0")) {
		// busca movimiento en movimientos por la entidad relacion
		query = "SELECT * FROM movimiento WHERE tipo_movimiento = "+parId_movim;
		//out.print(query+"<br>");
		StatementRs = ConnRs.prepareStatement(query);
		Rsquery = StatementRs.executeQuery();
		Rsquery_hasData = Rsquery.next();
		if (Rsquery_hasData) {
			bandera = "1";
		} else {
			bandera = "0";
		}
		//out.print("bandera -> "+bandera+"<br>");
	}

	if (bandera.equals("1")) {
		out.print("<script>");
		out.print("		alert(\""+mensaje+"\");");
		out.print("		history.go(-1);");
		out.print("</script>");
		Rsquery.close();
	} else {
		//--- elimina el ruta
		query= " DELETE tipo_movimiento WHERE id_movim = "+parId_movim;
		//out.print(query+"<br>");
		StatementRs=ConnRs.prepareStatement(query);
	   	StatementRs.executeUpdate();
	}//fin if(bandera.equals("1"))

} // if(parId_movim!=null && !parId_movim.equals(""))

	out.print("<script>");
	out.print("window.opener.location.reload();");
	out.print("window.close();");
	out.print("</script>");

ConnRs.close();
%>
<html>
<head>
<title>CIMA v2 - Eliminar Motivo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="miestilo.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<center>
<br>
  <table width="250" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td align="center">El motivo a sido eliminado.</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="center"> 
        <input type="button" name="Button" value="Aceptar" class="boton" onClick="window.opener.location.reload();window.close()">
      </td>
    </tr>
  </table>
</center>
</body>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
</HEAD>
</html>
